Skip to content

Conversation

@fmeum
Copy link
Collaborator

@fmeum fmeum commented Oct 29, 2025

Files.copy uses various optimizations such as kernel buffers (sendfile on Unix) or copy-on-write (clonefile on macOS, copy_file_range on Linux with a supported file system). InputStream.transferTo can use kernel buffers when both input streams are FileInputStreams.

@fmeum
Copy link
Collaborator Author

fmeum commented Oct 29, 2025

Stacked on #27458.

@fmeum fmeum force-pushed the nio-copy branch 2 times, most recently from ce89326 to 6591ac3 Compare October 29, 2025 12:53
@fmeum fmeum marked this pull request as ready for review October 29, 2025 13:40
@fmeum fmeum requested a review from tjgq October 29, 2025 13:40
@github-actions github-actions bot added the awaiting-review PR is awaiting review from an assigned reviewer label Oct 29, 2025
throw translateNioToIoException(from.asFragment(), e);
}
return;
}
Copy link
Contributor

@tjgq tjgq Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also replace ByteStreams.copy(in, out) with in.transferTo(out) below? It can still do in-kernel I/O when both sides are files, and falls back to essentially equivalent code otherwise.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that and realized that the copy fallback in moveFile can also be optimized in this way.

@fmeum fmeum changed the title Implement FileSystemUtils.copyFile via Files.copy if possible Optimize file copies by using NIO methods Oct 30, 2025
@fmeum fmeum requested review from Copilot and tjgq October 30, 2025 19:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors exception handling in the VFS implementation to centralize and standardize error message formatting across different FileSystem implementations. The key changes introduce a new translateNioToIoException utility method that maps NIO exceptions to consistent java.io exceptions with Unix-style error messages.

  • Centralized exception translation logic in FileSystem.translateNioToIoException()
  • Refactored getIoFile() and getNioPath() methods to return null instead of throwing UnsupportedOperationException
  • Optimized file copy operations to use NIO fast paths when available

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
FileSystem.java Added centralized translateNioToIoException() method and error message constants; changed getIoFile()/getNioPath() to return null instead of throwing exceptions
JavaIoFileSystem.java Simplified exception handling in createSymbolicLink(), readSymbolicLink(), and renameTo() to use the new translation method; removed duplicate error constants
WindowsFileSystem.java Updated createSymbolicLink() to use centralized exception translation
PathTransformingDelegateFileSystem.java Added @Nullable annotations to getIoFile() and getNioPath() methods
FileSystemUtils.java Added fast path for file copying using NIO APIs; replaced custom buffer copying with transferTo(); removed copyLargeBuffer() utility method
AbstractFileSystem.java Added null-check wrapper for getIoFile() call; removed duplicate ERR_PERMISSION_DENIED constant
Comments suppressed due to low confidence (1)

src/main/java/com/google/devtools/build/lib/vfs/AbstractFileSystem.java:99

  • The getNioPath(path) method now returns @Nullable as per the changes, but this code doesn't handle the null case. If getNioPath() returns null, this will throw a NullPointerException when passed to Files.newByteChannel(). A null check should be added to handle file systems that don't support NIO paths.
      return Files.newByteChannel(getNioPath(path), READ_WRITE_BYTE_CHANNEL_OPEN_OPTIONS);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@iancha1992 iancha1992 added the team-Performance Issues for Performance teams label Oct 30, 2025
fmeum added 2 commits October 31, 2025 09:14
`Files.copy` uses various optimizations such as kernel buffers (`sendfile` on Unix) or copy-on-write (`clonefile` on macOS, `copy_file_range` on Linux with a supported file system).
Copy link
Contributor

@tjgq tjgq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also amend the testGetNioPath_* tests in FileSystemTest? They no longer need to account for the UnsupportedOperationException case.

@fmeum fmeum requested a review from tjgq October 31, 2025 13:02
@tjgq
Copy link
Contributor

tjgq commented Oct 31, 2025

Importing this one myself.

@github-actions github-actions bot removed the awaiting-review PR is awaiting review from an assigned reviewer label Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-Performance Issues for Performance teams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants